<?php
// +----------------------------------------------------------------------
// | YZCMS <XueHen CMS>
// +----------------------------------------------------------------------
// | Copyright (c) http://www.xuehen.org
// +----------------------------------------------------------------------
// | Author XueHen <490860758@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;


class MapController extends BaseController{
    function index(){
		
		$this->display();
    }
	
	function map(){
		if(I('type')){
			$type =  I('type');
			
			switch($type){
				case 1:
				$this->mapOne();
				break;
				case 2:
				echo '此版本不支持！';
				break;
				default:
				$this->error('请选择方案！');
				break;
			}
		}else{
			$this->error('请选择方案！');
		}
	}
	
	function mapOne(){
		$content = $this->fetch('map');
		$str = '';
		$xml = '<urlset>';
		$xml .= '<url>';
		$xml .= '<loc>'.F('SiteDomain').'</loc>';
		$xml .= '<changefreq>daily</changefreq>';
		$xml .= '<priority>1.0</priority>';
		$xml .= '</url>';
		$db = M('category');
		$list = $db->where(array('type'=>'page'))->select();
		foreach($list as $v){
			$str .= '<h3><a href="/'.($v['url'] ? $v['url'] : $v['id']).'/">'.$v['name'].'</a></h3>';
			$xml .= '<url>';
			$xml .= '<loc>'.F('SiteDomain').'/'.($v['url'] ? $v['url'] : $v['id']).'/</loc>';
			$xml .= '<changefreq>monthly</changefreq>';
			$xml .= '<priority>0.8</priority>';
			$xml .= '</url>';
		}
		$content = str_replace('{$page}',$str,$content);
		$str = '';
		$list = $db->where(array('type'=>array('neq','page')))->select();
		$db = M('content');
		foreach($list as $v){
			$str .= '<h2><a href="/'.($v['url'] ? $v['url'] : $v['id']).'/">'.$v['name'].'</a></h2>';
			
			$xml .= '<url>';
			$xml .= '<loc>'.F('SiteDomain').'/'.($v['url'] ? $v['url'] : $v['id']).'/</loc>';
			$xml .= '<changefreq>monthly</changefreq>';
			$xml .= '<priority>0.8</priority>';
			$xml .= '</url>';
			
			$str .='<ul>';
			$re = $db->where(array('catid'=>$v['id']))->select();
			foreach($re as $va){
				$str .='<li><a href="/read/'.($va['url'] ? $va['url'] : $va['id']).'/">'.$va['title'].'</a></li>';
				
				$xml .= '<url>';
				$xml .= '<loc>'.F('SiteDomain').'/read/'.($va['url'] ? $va['url'] : $va['id']).'/</loc>';
				$xml .= '<changefreq>monthly</changefreq>';
				$xml .= '<priority>0.6</priority>';
				$xml .= '</url>';
			}
			$str .='</ul>';
		}
		$xml .= '</urlset>';
		$content = str_replace('{$content}',$str,$content);
		$file = __ROOT__.'map.html';
		$fp = fopen($file,'w');
		fwrite($fp,$content);
		$fp = fopen(__ROOT__.'sitemap.xml','w');
		fwrite($fp,$xml);
		$this->success('生成成功');
		
	}
	
}